<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <link rel="stylesheet" href="../static/js/prettify/prettify.css"/>
    <style>
        .demo {
            margin-bottom: 20px;
            padding: 20px;
            border-bottom: 1px solid #1b926c;
        }

        input.ng-invalid {
            border: 3px solid red;
        }
        input.ng-valid {
            border: 3px solid green;
        }

        .error{
            color:red;
            font-weight: bold;
        }
        /*.navbar{*/
        /*border:1px solid #1b926c;*/
        /*background-color: #1fa67a;*/
        /*}*/

    </style>
</head>
<body>

<div class="container">
    <div class="row">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="../index.html">首页</a>
                </div>
                <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                </ul>
            </div>
        </nav>
    </div>
    <div ng-app="myApp">
        <div class="row demo">
            <h3>Demo1: 表单验证</h3>
            <form name="signup_form"
                  novalidate
                  ng-submit="signupForm()"
                  ng-controller="SignupController">
                <fieldset>
                    <legend>Signup</legend>
                    <div class="row">
                        <div class="col-md-12">
                            <label>Your name</label>
                            <input ng-class="{error: signup_form.name.$dirty && signup_form.name.$invalid}"
                                   type="text"
                                   placeholder="Name"
                                   class="form-control"
                                   name="name"
                                   ng-model="signup.name"
                                   ng-minlength="3"
                                   ng-maxlength="20" required ng-focus />

                            <div class="error"
                                 ng-show="signup_form.name.$dirty && signup_form.name.$invalid && !signup_form.name.$focused">
                                <small class="error"
                                       ng-show="signup_form.name.$error.required">
                                    Your name is required.
                                </small>
                                <small class="error"
                                       ng-show="signup_form.name.$error.minlength">
                                    Your name is required to be at least 3 characters
                                </small>
                                <small class="error"
                                       ng-show="signup_form.name.$error.maxlength">
                                    Your name cannot be longer than 20 characters
                                </small>
                            </div>
                        </div>
                    </div>
                    <br/>
                    <button type="submit" class="btn btn-default">Submit</button>
                </fieldset>
            </form>

        </div>
    </div>
</div>
</body>
</html>
<script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script src="../static/js/prettify/prettify.min.js"></script>
<script>
    /**
     * pretiffy美化代码
     */
    $(function () {
        $('pre').addClass('prettyprint linenums').attr('style', 'overflow:auto');
        window.prettyPrint && prettyPrint();
    });
</script>
<script>
    var myApp = angular.module("myApp", []);
    myApp.controller('SignupController', function($scope) {
        $scope.submitted = false;
        $scope.signupForm = function() {
            if ($scope.signup_form.$valid) {
                //正常提交
                alert("通过验证，提交表单了");
            } else {
                $scope.signup_form.submitted = true;
            }
        }
    });

    /**
     * ngFocus指令给表单输入字段的blur和focus添加了对应的行为，添加了一个名为ng-focused的类，并将$focused的值设置为true。
     */
    myApp.directive('ngFocus', [function() {
        var FOCUS_CLASS = "ng-focused";
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, element, attrs, ctrl) {
                ctrl.$focused = false;
                element.bind('focus', function(evt) {
                    element.addClass(FOCUS_CLASS);
                    scope.$apply(function() {
                        ctrl.$focused = true;
                    });
                }).bind('blur', function(evt) {
                    element.removeClass(FOCUS_CLASS);
                    scope.$apply(function() {
                        ctrl.$focused = false;
                    });
                });
            }
        };
    }]);
</script>
